Ext.form.expColumnField = Ext.extend(Ext.form.TriggerField, {
	initComponent : function() {

		this.dataRecord = Ext.data.Record.create([
		                              			{name : 'tablecode',type : 'string'},
		                            			{name : 'name',	type : 'string'},
		                            			{name : 'cnname',type : 'string'},
		                            			{name : 'exptable',type : 'string'},
		                            			{name : 'sourcetable',	type : 'string'},
		                            			{name : 'sourcecolumn',	type : 'string'},
		                            			{name : 'expcol',type : 'string'}
		                            			]);
		this.tablestore = new Ext.data.Store({
			par : this,
			proxy : new Ext.data.HttpProxy({
						url :basepath+'../sysControl/system/SysDicColumnController/comboQuery.do'

						}),

				reader : new Ext.data.JsonReader({
							totalProperty : 'results',
							id:'name',
							}, this.dataRecord)
		});
		this.targetsotre = new Ext.data.Store({
			reader : new Ext.data.JsonReader({
				totalProperty : 'results',
					id:'name',
			}, this.dataRecord)
		});
		this.sm = new Ext.grid.CheckboxSelectionModel({
			singleSelect : false
		});
		this.sm2 = new Ext.grid.CheckboxSelectionModel({
			singleSelect : false
		});
		this.cm = new Ext.grid.ColumnModel([ this.sm, {
			id : 'name',
			header : '字段名称',
			dataIndex : 'name',
			width : 100,
			sortable : true
		} , {
			id : 'cnname',
			header : '中文名',
			dataIndex : 'cnname',
			width : 100,
			sortable : true
		} ]);
		this.cmCombo=new Ext.data.Store({
    		autoLoad:true,
			reader : new Ext.data.JsonReader({
				id : "columnid"
			}, Ext.data.Record.create([ {
				name : "tablename",
				type : 'string'
			}, {
				name : "tablecnname",
				type : 'string'
			}]))
			
		});
		this.cm2 = new Ext.grid.ColumnModel([ this.sm, {
			id : 'name',
			header : '字段名称',
			dataIndex : 'name',
			width : 100,
			sortable : true
		},{
			id : 'sourcetable',
			header : '扩展表',
			dataIndex : 'sourcetable',
			width : 100,
			sortable : true
		},{
			id : 'sourcecolumn',
			header : '扩展字段',
			dataIndex : 'sourcecolumn',
			width : 100,
			sortable : true
		} ]);
		this.sourcegrid = new Ext.grid.GridPanel({
			region : 'west',
			cm : this.cm,
			sm : this.sm,
			width : 240,
			height : 500,
			store : this.tablestore
		});
		this.targetgrid = new Ext.grid.GridPanel({
			region : 'east',
			cm : this.cm2,
			sm : this.sm2,
			width : 360,
			height : 500,
			store : this.targetsotre
		});
		this.colpanel = new Ext.Panel({

			par : this,
			style : 'text-align:center;',
			region : 'center',
			padding : "60px 0px 0px 0px",
			frame : true,
			items : [ {
				xtype : 'button',
				icon : basepath + "/image/right2.gif",
				handler : this.addRow,
				ownerCt : this
			}, {
				xtype : 'button',
				icon : basepath + "/image/left2.gif",
				handler : this.removeRow,
				ownerCt : this
			}, {
				xtype : 'button',
				iconCls : 'table_row_insert',
				ownerCt : this,
				handler : this.allToRight
			}, {
				xtype : 'button',
				iconCls : 'table_row_delete',
				ownerCt : this,
				handler : this.allToLeft
			} ]
		})
		this.setAllValue = function() {
			var displaytext = "";
			var hiddentext = "[";
			for ( var i = 0, n = this.targetsotre.getCount(); i < n; i++) {
				var record = this.targetsotre.getAt(i);
				if (i == 0) {
					displaytext = record.data.cnname;
					hiddentext += "{name:"+(record.data.name==""?null:"'"+record.data.name+"'")+",sourcetable:"+(record.data.sourcetable==""?null:"'"+record.data.sourcetable+"'")+",sourcecolumn:"+(record.data.sourcecolumn==""?null:"'"+record.data.sourcecolumn+"'")+",tablecode:'"+record.data.tablecode+"'"+"}";
				} else {
					displaytext += "," + record.data.cnname;
					hiddentext += "," + "{name:"+(record.data.name==""?null:"'"+record.data.name+"'")+",sourcetable:"+(record.data.sourcetable==""?null:"'"+record.data.sourcetable+"'")+",sourcecolumn:"+(record.data.sourcecolumn==""?null:"'"+record.data.sourcecolumn+"'")+",tablecode:'"+record.data.tablecode+"'"+"}";
				}
			}
			this.setValue(displaytext);
			this.tableField.value = hiddentext+"]";
			alert(this.tableField.value)
			this.tableWindow.hide();
		}
		this.initStore = function(store, records) {
			store.par.targetsotre.removeAll();
			if (store.par.tableField.value == ""
					|| store.par.tableField.value == undefined)
				return;
			var arrStr = store.par.tableField.value;
			var arr=eval(arrStr);
			for ( var i = 0; i < arr.length; i++) {
				var record = store.par.getRecord(store.par.tablestore, arr[i]);
				if (record) {
					store.par.targetsotre.add(record);
					store.par.tablestore.remove(record)
				}
			}
		};
		this.initFieldValue = function(value) {
			
			this.tablestore.baseParams.tablenames=Ext.getCmp(this.tableCmp).getValue();
			this.tablestore.load();
		}
		
		this.tablestore.on("load", this.initStore);
		this.getRecord = function(store, json) {
			for ( var i = 0, n = store.getCount(); i < n; i++) {
				if (store.getAt(i).data.tablecode == json.tablecode&&store.getAt(i).data.name==json.name) {
					return store.getAt(i);
				}
			}
		}
		this.buttonSave = {
			text : "保存",
			par : this,
			handler : function() {
				this.par.setAllValue()
			}
		};
		this.buttonCancel = {
			text : "关闭",
			par : this,
			handler : function() {
				this.par.tableWindow.hide();
			}
		}
		
		this.tableWindow = new Ext.Window({
			title : "扩展字段选择",
			width : 650,
			height : 400,
			layout : 'border',
			closeAction : 'hide',
			plain : true,
			// actionUrl : url,
			labelAlign : "right",
			buttonAlign : "center",
			resizable : false,
			modal : true,
			padding : "2px 2px",
			items : [ this.sourcegrid, this.colpanel, this.targetgrid ],

		});
		this.tableWindow.addButton(this.buttonSave);
		this.tableWindow.addButton(this.buttonCancel);
		this.onTriggerClick = function() {
			if(!Ext.getCmp(this.tableCmp).getValue())
				return ;
			this.initFieldValue();
			this.tableWindow.show();
		}

		Ext.form.expColumnField.superclass.initComponent.call(this);
	},
	// private
	onRender : function(ct, position) {
		Ext.form.expColumnField.superclass.onRender.call(this, ct, position);
		if (this.tablesname) {
			this.tableField = this.el.insertSibling({
				tag : 'input',
				type : 'hidden',
				name : this.tablesname,
				id : this.tablesname
			}, 'before', true);

		}
	},
	addRow : function() {
		var records = this.ownerCt.par.sourcegrid.getSelectionModel()
				.getSelections();
		var sourcegrid = this.ownerCt.par.sourcegrid;
		var targetgrid = this.ownerCt.par.targetgrid;
		var sourcestore = this.ownerCt.par.sourcegrid.getStore();
		var targetstore = this.ownerCt.par.targetgrid.getStore();
		if (records && records.length > 0) {
			targetstore.add(records);
			sourcestore.remove(records);
		}
	},
	removeRow : function() {
		var records = this.ownerCt.par.targetgrid.getSelectionModel()
				.getSelections();
		var sourcegrid = this.ownerCt.par.sourcegrid;
		var targetgrid = this.ownerCt.par.targetgrid;
		var sourcestore = this.ownerCt.par.sourcegrid.getStore();
		var targetstore = this.ownerCt.par.targetgrid.getStore();
		if (records && records.length > 0) {
			sourcestore.add(records);
			targetstore.remove(records);
			sourcegrid.refresh();
			targetgrid.refresh();
		}
	},
	allToRight : function() {
		var sourcestore = this.ownerCt.par.sourcegrid.getStore();
		var targetstore = this.ownerCt.par.targetgrid.getStore();
		var rsAll = sourcestore.getRange(0, sourcestore.getCount());
		sourcestore.removeAll();
		targetstore.add(rsAll);
	},
	allToLeft : function() {
		var sourcestore = this.ownerCt.par.sourcegrid.getStore();
		var targetstore = this.ownerCt.par.targetgrid.getStore();
		var rsAll = targetstore.getRange(0, targetstore.getCount());
		targetstore.removeAll();
		sourcestore.add(rsAll);
	}
})
Ext.reg('expcolumnfield', Ext.form.expColumnField);